const { shell, app, ipcMain } = require("electron");
const Store = require("electron-store");
const Types = require("../../const/eventName");

const settingStore = new Store({ name: "settings" });

const ossConfig = settingStore.get(Types.OSSCONFIG);
const ossEnable =
  ossConfig.region &&
  ossConfig.accessKeyId &&
  ossConfig.accessKeySecret &&
  ossConfig.bucket;

const isMac = process.platform === "darwin";

const template = [
  {
    label: "文件",
    submenu: [
      {
        label: "新建",
        accelerator: "CommandOrControl+N",
        click(meunuItem, browserWindow) {
          browserWindow.webContents.send(Types.CREATENEWFILE);
        },
      },
      {
        label: "保存",
        accelerator: "CommandOrControl+S",
        click(_, browserWindow) {
          browserWindow.webContents.send(Types.SAVEEDITFILE);
        },
      },
      {
        label: "搜索",
        accelerator: "CommandOrControl+F",
        click(_, browserWindow) {
          browserWindow.webContents.send(Types.SEARCHFILE);
        },
      },
      {
        label: "导入",
        accelerator: "CommandOrControl+I",
        click(_, browserWindow) {
          browserWindow.webContents.send(Types.IMPORTFILE);
        },
      },
    ],
  },
  {
    label: "编辑",
    submenu: [
      { role: "undo", label: "撤销", accelerator: "CommandOrControl+Y" },
      { role: "redo", label: "重做", accelerator: "Shift+CommandOrControl+Y" },
      { role: "separator" },
      { role: "cut", label: "剪切" },
      { role: "copy", label: "复制", accelerator: "CommandOrControl+C" },
      { role: "paste", label: "粘贴", accelerator: "CommandOrControl+P" },
    ],
  },
  {
    label: "视图",
    submenu: [
      {
        role: "reload",
        label: "刷新",
        accelerator: "CommandOrControl+R",
        click(item, focusWindow) {
          if (focusWindow) {
            focusWindow.webContents.reload();
          }
        },
      },
      {
        role: "forceReload",
        label: "强制刷新",
        accelerator: "Shift+CommandOrControl+R",
      },
      {
        role: "toggleDevTools",
        label: "开发者工具",
        click(item, focusWindow) {
          if (focusWindow) {
            focusWindow.toggelDevTools();
          }
        },
      },
      {
        role: "togglefullscreen",
        label: "全屏",
        click(item, focusWindow) {
          if (focusWindow) {
            focusWindow.setFullScreen(!focusWindow.isFullScreen);
          }
        },
      },
    ],
  },
  {
    label: "窗口",
    submenu: [
      {
        label: "最小化",
        role: "minimize",
        accelerator: "CommandOrControl+W",
      },
      { label: "最大化", role: "zoom", accelerator: "CommandOrControl+M" },
    ],
  },
  {
    label: "云同步",
    id: "ossMenu",
    submenu: [
      {
        label: "设置",
        id: "setting",
        accelerator: "Command+,",
        click() {
          ipcMain.emit(Types.SHOWSETTINGWINDOW);
        },
      },
      {
        id: "ossSync",
        label: "自动同步",
        type: "checkbox",
        enabled: ossEnable,
        checked: ossEnable && !!settingStore.get(Types.OSSSYNCINFO),
        click(item, focusWindow) {
          settingStore.set(Types.OSSSYNCINFO, item.checked);
          focusWindow.webContents.send(Types.OSSSYNCINFOCHANGE, item.checked);
        },
      },
      {
        id: "toOss",
        label: "全部同步到云端",
        enabled: ossEnable,
        click() {
          console.log("从云端下载到本地");
        },
      },
      {
        id: "toLocal",
        label: "从云端下载到本地",
        enabled: ossEnable,
        click() {
          console.log("从云端下载到本地");
        },
      },
    ],
  },
  {
    label: "帮助",
    submenu: [
      {
        label: "学习更多",
        click: async () => {
          await shell.openExternal("https://electronjs.org");
        },
      },
    ],
  },
];

if (isMac) {
  const appName = app.getName();
  template.unshift({
    label: appName,
    submenu: [
      { label: `关于${appName}`, role: "about" },
      { type: "separator" },
      {
        label: `设置`,
        accelerator: "Command+,",
        click() {
          ipcMain.emit(Types.SHOWSETTINGWINDOW);
        },
      },
      { label: `隐藏`, role: "hide" },
      { label: `隐藏其他`, role: "hideothers" },
      { label: `显示`, role: "unhide" },
      { type: "separator" },
      { label: `退出`, role: "quit" },
    ],
  });
}

module.exports = template;
